<?php
// +----------------------------------------------------------------------
// | [KyPHP System] Copyright (c) 2020 http://www.kuryun.com/
// +----------------------------------------------------------------------
// | [KyPHP] 并不是自由软件,你可免费使用,未经许可不能去掉KyPHP相关版权
// +----------------------------------------------------------------------
// | Author: fudaoji <fdj@kuryun.cn>
// +----------------------------------------------------------------------

/**
 * Created by PhpStorm.
 * Script Name: FormBuilder.php
 * Create: 2020/7/29 下午10:53
 * Description: 应用内的快捷表单
 * Author: fudaoji<fdj@kuryun.cn>
 */

namespace app\common\controller;

use app\admin\controller\FormBuilder;
use think\facade\View;

class AddonFormBuilder extends FormBuilder
{
    protected $_view_path = '';
    public function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub
        $this->_template = 'builder/form';
    }

    /**
     * 设置模版目录
     * @param string $view_path
     * @return $this
     * @author: fudaoji<fdj@kuryun.cn>
     */
    public function setViewPath($view_path = ''){
        $this->_view_path = $view_path;
        return $this;
    }

    /**
     * 显示页面
     * @param array $assign
     * @param string $view
     * @return mixed
     * @Author  Doogie<461960962@qq.com>
     */
    public function show($assign = [], $view = '') {
        //额外已经构造好的表单项目与单个组装的的表单项目进行合并
        $this->_form_items = array_merge($this->_form_items, $this->_extra_items);

        //编译表单值
        if ($this->_form_data) {
            foreach ($this->_form_items as &$item) {
                if (isset($this->_form_data[$item['name']])) {
                    $item['value'] = $this->_form_data[$item['name']];
                }
            }
        }

        $assign = array_merge([
            'meta_title' => $this->_meta_title,          // 页面标题
            'tab_nav' => $this->_tab_nav,             // 页面Tab导航
            'post_url' => $this->_post_url,         //提交的url
            'form_items' =>  $this->_form_items,  //表单项目
            'ajax_submit' => $this->_ajax_submit,  //是否ajax提交
            'extra_html' => $this->_extra_html,    // 额外HTML代码
            'tip'   => $this->_tip
        ], $assign, $this->assign);
        unset($this->_meta_title, $this->_tab_nav,$this->_post_url,$this->_form_items,$this->_ajax_submit,$this->_extra_html, $this->_tip);

        View::config('view_path', $this->_view_path);
        return view($this->_template, $assign);

    }
}